package SwingView;

import javax.swing.*;

import java.awt.*;
import java.awt.event.KeyEvent;

public class CmsLoginView extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField jtfUserName;
	private JPasswordField jtfPassword;
	private JbtLoginFrameButton jbtLogIn;
	private JbtLoginFrameButton jbtQuit;
	private JLabel jlNotification;
	
	public CmsLoginView(){
		//Instantiate the components
		jtfUserName = new JTextField();
		jtfPassword = new JPasswordField();
		jbtLogIn = new JbtLoginFrameButton(" Log In ");
		jbtQuit = new JbtLoginFrameButton(" Quit ");
		jlNotification = new JLabel("Please input your username and password");
		
		//Set up Main Panel
		JpLoginPanel jpMain = new JpLoginPanel();
		
		GridBagConstraints cJpMain = new GridBagConstraints();
		cJpMain.anchor = GridBagConstraints.CENTER;
		
		//Add Component Panel to  Login Frame:
		setLayout(new GridBagLayout());
		add(jpMain,cJpMain);
		
		//Set up Login Frame
		getContentPane().setBackground(new Color(41,41,41));//set background color for login frame
		setSize(new Dimension(1024,700)); // set default size to be 800x600 pixels
		setMinimumSize(new Dimension(1024,600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set default close operation
		setLocationRelativeTo(null);
	}
	
	public void notifyAuthenticationFail(){
		jlNotification.setText("Wrong user name or password!");
		jtfUserName.setText("");
		jtfPassword.setText("");
	}
	
	protected class JpLoginPanel extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public JpLoginPanel(){
			//Set up the Login Frame
			setLayout(new GridBagLayout());
			setOpaque(true);
			setBackground(new Color(131,0,0));
			setPreferredSize(new Dimension(330,270));
			
			//Add UMD Banner
			JLabel jlUMDBanner = new JLabel(new ImageIcon("./assets/bannerUMDSmall.png"));
			GridBagConstraints cUMDBanner = new GridBagConstraints();
			cUMDBanner.gridy = 0;
			cUMDBanner.gridwidth = 2;
			cUMDBanner.fill = GridBagConstraints.HORIZONTAL;
			cUMDBanner.anchor = GridBagConstraints.CENTER;
			cUMDBanner.weighty = 0.2;
			add(jlUMDBanner, cUMDBanner);
			
			//Add Username label
			JLabel jlUserName = new JLabel("Username:    ");
			GridBagConstraints cJlUserName = new GridBagConstraints();
			cJlUserName.gridx = 0;
			cJlUserName.gridy = 2;
			cJlUserName.weightx = 0.35;
			cJlUserName.anchor = GridBagConstraints.LINE_END;
			add(jlUserName,cJlUserName);
			
			//Add username textfield
			jtfUserName = new JTextField();
			jtfUserName.setPreferredSize(new Dimension(180,25));
			GridBagConstraints cJtfUserName = new GridBagConstraints();
			cJtfUserName.anchor = GridBagConstraints.LINE_START;
			cJtfUserName.weightx = 0.6;
			cJtfUserName.gridx = 1;
			cJtfUserName.gridy = 2;
			cJtfUserName.fill = GridBagConstraints.VERTICAL;
			add(jtfUserName,cJtfUserName);
			
			//Add Password label
			JLabel jlPassword = new JLabel("Password:    ");
			GridBagConstraints cJlPassword = new GridBagConstraints();
			cJlPassword.gridx = 0;
			cJlPassword.gridy = 3;
			cJlPassword.weighty = 0.3;
			cJlPassword.weightx = 0.35;
			cJlPassword.anchor = GridBagConstraints.LINE_END;
			add(jlPassword,cJlPassword);
			
			//Add password textfield
			jtfPassword.setPreferredSize(new Dimension(180,25));
			GridBagConstraints cJtfPassword = new GridBagConstraints();
			cJtfPassword.anchor = GridBagConstraints.LINE_START;
			cJtfPassword.weighty = 0.3;
			cJtfPassword.weightx = 0.6;
			cJtfPassword.gridx = 1;
			cJtfPassword.gridy = 3;
			add(jtfPassword,cJtfPassword);
			
			
			//Add Authentication notification label:
			GridBagConstraints cJlAuthenticationFail = new GridBagConstraints();
			cJlAuthenticationFail.gridx = 0;
			cJlAuthenticationFail.gridy = 1;
			cJlAuthenticationFail.gridwidth = 2;
			cJlAuthenticationFail.weighty = 0.35;
			cJlAuthenticationFail.anchor = GridBagConstraints.CENTER;
			add(jlNotification,cJlAuthenticationFail);
			
			//Add the LogIn and Quit button
			JPanel jpHelper = new JPanel(new FlowLayout(FlowLayout.CENTER,25,0));
			jpHelper.setOpaque(false);
			jpHelper.add(jbtLogIn);
			jpHelper.add(jbtQuit);
			
			//Add LogIn Button
			GridBagConstraints cJpHelper = new GridBagConstraints();
			cJpHelper.gridwidth = 2;
			cJpHelper.gridx = 0;
			cJpHelper.gridy = 4;
			cJpHelper.weighty = 0.4;
			cJpHelper.weightx = 0.5;
			cJpHelper.anchor = GridBagConstraints.CENTER;
			add(jpHelper,cJpHelper);

		}
	}
	
	//---------------------------------------------------------
	//Class
	// - Customized JButton for this frame
	// - Used for Log In button and Quit button
	//---------------------------------------------------------
	public class JbtLoginFrameButton extends JButton{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public JbtLoginFrameButton(String buttonTitle){
			super(buttonTitle);
			this.setBorder(BorderFactory.createEmptyBorder(3,15, 3, 15));
			this.setOpaque(true);
			this.setBackground(new Color(31,31,31));
			this.setForeground(Color.WHITE);
			this.setPreferredSize(new Dimension(110,30));
		}
	}
	
	//---------------------------------------------------------
	//GETTERS AND SETTERS:
	//---------------------------------------------------------
	public JTextField getJtfUserName() {
		return jtfUserName;
	}

	public void setJtfUserName(JTextField jtfUserName) {
		this.jtfUserName = jtfUserName;
	}

	public JPasswordField getJtfPassword() {
		return jtfPassword;
	}

	public void setJtfPassword(JPasswordField jtfPassword) {
		this.jtfPassword = jtfPassword;
	}

	public JbtLoginFrameButton getJbtLogIn() {
		return jbtLogIn;
	}

	public void setJbtLogIn(JbtLoginFrameButton jbtLogIn) {
		this.jbtLogIn = jbtLogIn;
	}

	public JbtLoginFrameButton getJbtQuit() {
		return jbtQuit;
	}

	public void setJbtQuit(JbtLoginFrameButton jbtQuit) {
		this.jbtQuit = jbtQuit;
	}

	public class CmsLoginTextField extends JTextField{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public CmsLoginTextField(){
			
		}
	}
}
